import React, {Component} from 'react';
import './index.less';
import {base, toast, high, promptBox} from 'nc-lightapp-front';
import initDieProcData from "../DieProcModal/functions/initDieProcData";
import initBlacklistData from "../BlacklistModal/functions/initBlacklistData";
import templatePrint from "../../container/functions/templatePrint";
import ReferSearch from "../../../../../../hrpub/common/components/referSearch/org";
import printOutput from "../../container/functions/printOutput";
import setQueryMeta from "../../container/functions/setQueryMeta";

const {NCCheckbox} = base;
const {PrintOutput} = high;

class Header extends Component {
    constructor(props) {
        super(props);

        this.state = {printData: {}};
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    onChangeHandle(type) {
        return (value) => {
            this.props.updateState({
                [type]: value,
                pageInfo: {
                    ...this.props.pageInfo,
                    pageIndex: 1
                }
            }, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'crosstrans':
                this.props.updateState({
                    page: 'crosstrans'
                });
                break;
            case 'query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('query', true);
                break;
            case 'sort':
                this.props.updateState({
                    sortModalVisible: true
                });
                break;
            case 'batchedit':
                const checkedRows = this.props.table.getCheckedRows('info');
                if (checkedRows.length === 0) {
                    toast({color: "warning", content: this.props.json['hi6007-000111']});
                    /* 国际化处理： 请勾选要批改的数据*/
                } else {
                    this.props.updateState({
                        batchEditModalVisible: true
                    });
                }
                break;
            case 'innertrans':
                const checkedRows3 = this.props.table.getCheckedRows('info');
                if (checkedRows3.length === 0) {
                    toast({color: "warning", content: this.props.json['hi6007-000160']});
                    /* 国际化处理： 请勾选要转移的离职人员*/
                } else {
                    this.props.updateState({
                        innerTransModalVisible: true
                    });
                }
                break;
            case 'dieproc':
                if (!this.props.selectedPsn) break;
                initDieProcData.call(this);
                break;
            case 'addblack':
                if (!this.props.selectedPsn) break;
                initBlacklistData.call(this);
                break;
            case 'refresh':
                if (this.props.hasSearched) {
                    this.props.updateState({
                        pageInfo: {
                            ...this.props.pageInfo,
                            pageIndex: 1
                        }
                    }, () => {
                        this.props.getMainTableData();
                    });
                }
                break;
            case 'viewctrt':
                const checkedRows2 = this.props.table.getCheckedRows('info');
                if (checkedRows2.length === 0) {
                    toast({color: "warning", content: this.props.json['hi6007-000161']});
                    /* 国际化处理： 请勾选要查看合同的数据*/
                } else {
                    this.props.updateState({
                        viewCtrtModalVisible: true
                    });
                }
                break;
            case 'print':
                templatePrint.call(this);
                break;
            case 'output':
                printOutput.call(this);
                break;
            case 'import':
                this.props.updateState({
                    showImportModalVisible: true
                });
                break;
            default:
                break;
        }
    }

    render() {
        const {button, isInJob, orgVal, disabledAll} = this.props;

        return (
            <div className="header">
                <div className="header-refer">
                    <ReferSearch
                        disabled={disabledAll}
                        orgVal={orgVal}
                        getOrgData={this.updateOrgValue}
                    />
                </div>
                <div className="header-checkbox-group">
                    <NCCheckbox
                        checked={isInJob}
                        className="header-checkbox"
                        color="primary"
                        onChange={this.onChangeHandle('isInJob')}
                        disabled={disabledAll}
                    >
                        {this.props.json['hi6007-000044']}{/* 国际化处理： 在职人员*/}
                    </NCCheckbox>
                </div>

                <div className="btn-group">
                    <div className="definition-icons">
                        {button.createButtonApp({
                            area: 'head',
                            onButtonClick: (props, btncode) => {
                                this.onBtnOperation(props, btncode, 'head', this);
                            }
                        })}
                    </div>
                </div>

                <PrintOutput
                    ref='printOutput'
                    url={'/nccloud/hrtrn/pub/RecordsPrintAction.do'}
                    data={this.state.printData}
                />
            </div>
        );
    }
}

export default Header;
